package com.haixee.parkdemo.auth.controller;

import com.haixee.parkdemo.auth.entity.request.UserRequest;
import com.haixee.parkdemo.auth.model.User;
import com.haixee.parkdemo.auth.service.UserService;
import com.haixee.parkdemo.auth.utils.TenantUtil;
import com.haixee.parkdemo.common.context.WebContextHolder;
import com.haixee.parkdemo.common.response.ResResult;
import com.haixee.parkdemo.common.response.ResUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public ResResult add(@RequestBody UserRequest user) {
        userService.addUser(user.getUsername(), user.getPassword(), user.getRoleIdList());
        return ResUtil.success();
    }

    @PostMapping("/group/update")
    public ResResult groupUpdate(@RequestBody UserRequest request) {

        userService.updateGroupJob(request.getUid(), request.getProjectJobIdList());
        return ResUtil.success();
    }


    @GetMapping("/list")
    public ResResult userList(Integer tenantId, Integer orgId, Integer page, Integer size) {
        return ResUtil.success(userService.getUserDTOList(TenantUtil.check(tenantId), orgId, page, size));
    }

    @GetMapping("/detail")
    public ResResult getUserDetail(Integer uid, Integer tenantId, Integer orgId) {
        return ResUtil.success(userService.getUserRoleDTO(uid, TenantUtil.check(tenantId), orgId));
    }

    @PostMapping("/role")
    public ResResult getUserDetail(@RequestBody UserRequest request) {
        userService.updateUserRole(request.getUid(), request.getRoleIdList());
        return ResUtil.success();
    }
}
